/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.util.Date;
import nl.cloudfarming.client.messaging.Message;
import org.junit.Test;
import org.uispec4j.UISpec4J;
import org.uispec4j.Panel;
import org.uispec4j.finder.ComponentMatcher;
import org.junit.Assert;
import org.uispec4j.Trigger;
import org.uispec4j.Window;
import org.uispec4j.interception.WindowInterceptor;

/**
 *
 * @author Merijn Zengers
 */
public class MessageListIT {

    private final InboxRowComponentMatcher inboxRowComponentMatcher = new InboxRowComponentMatcher();

    static {
        UISpec4J.init();
    }

    /**
     * Test of addMessage method, of class MessageListPanel.
     */
    @Test
    public void testInsertMessage() {
        Message.Builder messageBuilder = new Message.Builder();
        Date dateReceived = new Date();
        messageBuilder.setOrigin("from").setTitle("title").setDateReceived(dateReceived).setMessageText("messageText");

        MessageListPanel instance = new MessageListPanel();

        Panel visualPanel = new Panel(instance);

        //Test if the panel does not already conatains a InboxRow
        Assert.assertFalse(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());

        instance.addMessage(messageBuilder.build());

        //Test if the panel contains a InboxRow
        Assert.assertTrue(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());
    }

    /**
     * Test of getSelectedInboxRow method, of class MessageListPanel.
     */
    @Test
    public void testGetSelectedMessage() {
        Message.Builder messageBuilder = new Message.Builder();
        Date dateReceived = new Date();
        messageBuilder.setOrigin("from").setTitle("title").setDateReceived(dateReceived).setMessageText("messageText");

        Message message = messageBuilder.build();

        MessageListPanel instance = new MessageListPanel();

        Panel visualPanel = new Panel(instance);

        //Test if the panel does not already conatains a InboxRow
        Assert.assertFalse(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());

        instance.addMessage(messageBuilder.build());

        //Test if the panel contains a InboxRow
        Assert.assertTrue(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());

        //Set selected on the InboxRow
        ((InboxRow) visualPanel.getSwingComponents(inboxRowComponentMatcher)[0]).focusGained(new FocusEvent(instance, 0));

        //Test if the selected message is our message
        Assert.assertEquals(message, instance.getSelectedInboxRow().getMessage());
    }

    /**
     * Test of focusGained method, of class MessageListPanel.
     * Tests if dubleclick triggers popup
     */
    //FIXME: Windowinterceptor gives headles ecception
    @Test
    public void testMouseClicked() {
        Message.Builder messageBuilder = new Message.Builder();
        Date dateReceived = new Date();
        messageBuilder.setOrigin("from").setTitle("title").setDateReceived(dateReceived).setMessageText("messageText");

        MessageListPanel instance = new MessageListPanel();

        Panel visualPanel = new Panel(instance);

        //Test if the panel does not already conatains a InboxRow
        Assert.assertFalse(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());

        instance.addMessage(messageBuilder.build());

        //Test if the panel contains a InboxRow
        Assert.assertTrue(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());

        //Set selected on the InboxRow
        ((InboxRow) visualPanel.getSwingComponents(inboxRowComponentMatcher)[0]).focusGained(new FocusEvent(instance, 0));
        
        Trigger trigger = new InboxRowTrigger(instance);
        Window window =  WindowInterceptor.run(trigger);
        Assert.assertNotNull(window);
    }

    class InboxRowComponentMatcher implements ComponentMatcher {

        @Override
        public boolean matches(Component component) {
            return (component instanceof InboxRow);
        }
    }

    class InboxRowTrigger implements Trigger {

        private MessageListPanel inbox;

        public InboxRowTrigger(MessageListPanel inbox) {
            this.inbox = inbox;
        }

        @Override
        public void run() throws Exception {
            //Make mouseClickEvent with two clicks
            MouseEvent event = new MouseEvent(inbox, 0, 0, 0, 0, 0, 2, false);
            inbox.mouseClicked(event);
        }
    }
}
